package com.sosee.cgfw.gaodemap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.navi.model.NaviLatLng;
import com.sosee.cgfw.R;
import com.sosee.cgfw.adapter.SpinnerAdapter;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/2/4.
 * 导航activity
 */
public class NewTestActivity extends Activity implements LocationSource,
        AMapLocationListener,RadioGroup.OnCheckedChangeListener {
    private Spinner sp_method;//选择导航方式
    private SpinnerAdapter mAdapter;//适配器
    private List<String> mList=new ArrayList<String>();//导航方式集合
    private AMap aMap;
    private MapView mapView;
    private LocationSource.OnLocationChangedListener mListener;
    private LocationManagerProxy mAMapLocationManager;
    private double longtitude;//当前位置精度
    private double latitude;//当前位置维度
    private TextView tv_end;//终点

    private ImageView ig_jiache;//驾车
    private ImageView ig_buxing;//步行
    private boolean isDriver=true;//是否为驾车模式
    private ImageView ig_xiala;//下拉按钮图片
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_display_overview_mode);
        mapView = (MapView) findViewById(R.id.navi_view);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        init();


    }

    public void back(View view){
        finish();
    }


    /**
     * 初始化
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
        tv_end= (TextView) findViewById(R.id.end_position_textview);
        tv_end.setText(getIntent().getStringExtra("address"));
        ig_jiache= (ImageView) findViewById(R.id.ig_jiache);
        ig_buxing= (ImageView) findViewById(R.id.ig_buxing);
        ig_jiache.setImageResource(R.drawable.jiachexuanzhong);
        ig_xiala= (ImageView) findViewById(R.id.ig_xiala);
        tv_policy= (TextView) findViewById(R.id.tv_policy);
    }

    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        aMap.setLocationSource(this);// 设置定位监听
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        //设置定位的类型为定位模式 ，可以由定位、跟随或地图根据面向方向旋转几种
        aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch(checkedId){
            case R.id.gps_locate_button:
                //设置定位的类型为定位模式
                aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
                break;
            case R.id.gps_follow_button:
                //设置定位的类型为 跟随模式
                aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
                break;
            case R.id.gps_rotate_button:
                //设置定位的类型为根据地图面向方向旋转
                aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
                break;
        }

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
        deactivate();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 此方法已经废弃
     */
    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    /**
     * 定位成功后回调函数
     */
    @Override
    public void onLocationChanged(AMapLocation aLocation) {

        if (mListener != null && aLocation != null) {
            LogUtils.log_E("定位信息："+aLocation.getLatitude()+":"+aLocation.getLongitude());
            NaviLatLng end= Utils.eNlat;
            AMapLocation loc=new AMapLocation("");
            loc.setLatitude(end.getLatitude());
            loc.setLongitude(end.getLongitude());

            mListener.onLocationChanged(aLocation);// 显示系统小蓝点
            MarkerOptions mo=new MarkerOptions();
            mo.position(new LatLng(end.getLatitude(),end.getLongitude()));
            mo.title("西安市");
            mo.snippet("气泡的文字");
            mo.perspective(true);
            mo.draggable(true);
            aMap.addMarker(mo);











            double lon=aLocation.getLongitude();
            double lat=aLocation.getLatitude();
            longtitude=lon;
            latitude=lat;

        }
    }















    /**
     * 激活定位
     */
    @Override
    public void activate(LocationSource.OnLocationChangedListener listener) {
        mListener = listener;
        if (mAMapLocationManager == null) {
            mAMapLocationManager = LocationManagerProxy.getInstance(this);
			/*
			 * mAMapLocManager.setGpsEnable(false);
			 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
			 * API定位采用GPS和网络混合定位方式
			 * ，第一个参数是定位provider，第二个参数时间最短是2000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
			 */
            mAMapLocationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, this);
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mAMapLocationManager != null) {
            mAMapLocationManager.removeUpdates(this);
            mAMapLocationManager.destory();
        }
        mAMapLocationManager = null;
    }















    public void select(View view){
        int flag=0;
        int id=view.getId();
        if(id==R.id.ig_guihua) {
            if(isDriver){
               if(policy.equals("费用优先")){
                   flag=Constans.JIACHE_MONE;
               }else if(policy.equals("躲避拥堵")){
                   flag=Constans.JIACHE_YONGDU;
               }else {
                   flag=Constans.JIACHE_FAST;
               }
            }else{
                flag=Constans.BUXING;
            }
            Intent routeIntent = new Intent(this, SimpleNaviRouteActivity.class);
            routeIntent.putExtra("flag", flag);
            routeIntent.putExtra("long", longtitude);
            routeIntent.putExtra("lat", latitude);
            routeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(routeIntent);
        }else {
            selectMode();
        }
    }

    public void selectMode(){
        if(isDriver){
            isDriver=false;
            ig_jiache.setImageResource(R.drawable.jiache);
            ig_buxing.setImageResource(R.drawable.buxingxuanzhong);
        }else{
            isDriver=true;
            ig_jiache.setImageResource(R.drawable.jiachexuanzhong);
            ig_buxing.setImageResource(R.drawable.buxing);
        }
    }

    private boolean isShow=false;//判断下拉框是否展开   默认未展开
    PopupWindow popupWindow;
    public void showPopUp(View v) {
        if(!isShow){
            showPopWindow(v);
            ig_xiala.setImageResource(R.drawable.dropup);
        }else{
            popupWindow.dismiss();
            ig_xiala.setImageResource(R.drawable.dropdown);
        }

    }

    public void showPopWindow(View v){
        LinearLayout layout= (LinearLayout) View.inflate(this,R.layout.item_pop_daohang,null);

        popupWindow = new PopupWindow(layout, v.getWidth()
                , ViewGroup.LayoutParams.WRAP_CONTENT);

        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//        int popupWidth = layout.getMeasuredWidth();
//        int popupHeight = layout.getMeasuredHeight();



        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        int[] location=new int[2];
        v.getLocationOnScreen(location);


        LogUtils.log_E(location[1] + "");
        LogUtils.log_E(popupWindow.getHeight()+"");

        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]+v.getHeight());
    }

    private TextView tv_policy;//策略内容
    private String policy="费用优先";//驾车 策略
    public void selectPolicy(View view){
        TextView tv= (TextView) view;
        policy=tv.getText().toString();
        Toast.makeText(this,policy,Toast.LENGTH_LONG).show();
        popupWindow.dismiss();
        tv_policy.setText(policy);
    }

}
